﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace NDBGen.Tests
{
    public static class ParsedInputUtil
    {
        public static ParsedInput CreateParsedInput(String tableInput, String scriptedDataPath = null, String scriptedDataInput = null)
        {
            var dataInput = scriptedDataPath == null ? null : new Dictionary<String, String[]> { { "test", new String[] { scriptedDataInput } } };
            return CreateParsedInput(tableInput, scriptedDataPath, dataInput);
        }

        public static ParsedInput CreateParsedInput(String tableInput, String scriptedDataPath, IDictionary<String, String[]> scriptedDataInput)
        {
            var scriptedData = new IGrouping<String, ScriptedTableData>[0];
            if (scriptedDataInput != null && scriptedDataInput.Any())
            {
                if (String.IsNullOrEmpty(scriptedDataPath))
                {
                    throw new ArgumentNullException("The path must be provided so that embedded file paths may be mapped.", "scriptedDataPath");
                }

                scriptedData = (
                    from inputs in scriptedDataInput
                    from input in inputs.Value
                    let data = ParserAccessor.ParseScriptedData(scriptedDataPath, input)
                    from entry in data
                    group entry by inputs.Key).ToArray();
            }

            var tables = ParserAccessor.ParseTables(tableInput, scriptedData);

            return new ParsedInput(tables.ToArray(), scriptedData);
        }
    }
}